//
//  MainViewController.m
//  TestApp
//
//  Created by 杨志涛 on 15/9/2.
//  Copyright (c) 2015年 杨志涛. All rights reserved.
//

#import "MainViewController.h"
#import "UIBarButtonItem+CommomBarButtonItem.h"
#import "AppConfig.h"

@interface MainViewController ()<UIGestureRecognizerDelegate>

@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}
#pragma mark - delegate
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {

    
}


#pragma mark 返回上一级
- (void)return
{
    // 最低控制器无需返回
    if (self.viewControllers.count <= 1) return;
    
    // pop返回上一级
    [self popToRootViewControllerAnimated:YES];
}


-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    NSLog(@"--push---viewControllers:%ld",self.viewControllers.count);
    
    [super pushViewController:viewController animated:animated];
    
    [self configController:viewController];
    
}
#pragma mark -
- (void)configController:(UIViewController *)viewController
{
    
    if(self.viewControllers.count == 1)
    {
        
        viewController.navigationItem.leftBarButtonItem = nil;
        return;
    }
    
    viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem createBackBBTframe:CGRectMake(0, 0, 50, viewController.navigationController.navigationBar.bounds.size.height) isShowArrow:YES isShowTitle:NO target:self action:@selector(back:)];
    
    
}

- (void)back:(UIViewController*)viewController
{
    // 最低控制器无需返回
    
    
    NSLog(@"--back---viewControllers:%ld",self.viewControllers.count);
    
//    if(self.viewControllers.count == 2)
//    {
////        viewController.navigationItem.leftBarButtonItem = nil;
//        return;
//    }

    
    [self popViewControllerAnimated:YES];
}



- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    /**
     *  全局手势返回
     */
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;      // 手势有效设置为YES  无效为NO
        self.navigationController.interactivePopGestureRecognizer.delegate = self;    // 手势的代理设置为self
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
